import decimal

# 2.4
print(-2 * 4 + 3 ** 2)
print(2 < 4)
print(2 == 4)
print(6.2 <= 6)
print(6.2 <= 6.2)
print(6.2 == 6.20001)
print(2 < 4 and 2 == 4)
print(2 > 4 or 2 < 4)
# 合理增加括号增加代码可读性

# 2.5
n = 2
n *= 3
print(n)
n = 1.1
print(n)

# 2.6
print(decimal.Decimal('1.1'))

# 2.7 字符串
pystr = 'Python'
iscool = 'is cool!'
print(pystr[0])
print(pystr[2:5])
print(iscool[:2])
print(iscool[3:])
print(iscool[-1])
print(pystr + ' ' + iscool)
print(pystr * 2)
print('_' * 20)
pystr = '''python
  is cool'''
print(pystr)

# 2.8 列表和元组
aList = [1, 2, 3, 4]
print(aList)
print(aList[0])
print(aList[2:])
print(aList[:3])
aList[1] = 5
print(aList)

aTuple = ('robots', 77, 93, 'try')
print(aTuple)
print(aTuple[:3])
# TypeError: 'tuple' object does not support item assignment
# aTuple[1] = 71
print(aTuple)
bTuple = 1, 'go', 99, 'whoop'
print('bTuple type is:' + str(type(bTuple)))

# 2.9 字典
aDict = {'host': 'earth'}
aDict['port'] = 80
print(aDict)
for key in aDict:
    print(key, aDict[key])

# 2.12 循环
counter = 0
while counter < 3:
    print('loop #%d' % counter)
    counter += 1

# 2.13 for循环和range()
for item in ['email', 'network', 'homework']:
    print(item)

print('We are the %s who say %s' % \
      ('knights', 'Ni! ' * 4))

for eachNum in range(3):
    print(eachNum)

# 循环元素
for c in 'abc':
    print(c)
foo = 'abc'
# 循环索引
for i in range(len(foo)):
    print(foo[i], '(%d)' % i)
# 循环索引和元素
for i, ch in enumerate(foo):
    print(ch, '(%d)' % i)

# 列表解析
for i in [x ** 2 for x in range(8)]:
    print(i)

print('-' * 20)
for i in [x ** 2 for x in range(8) if x % 2]:
    print(i)
